package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DBUtils {
	
	public static String  path = "";   
    public static String url = "";  
    public static String driver = "";  
    public static String user = "";  
    public static String password = "";  
    static{
    	 path = PathUitls.getFilePathOfResource("all.properties");
    	 if(path==null){
    		 throw new NullPointerException("��ȡ�����ļ�·�������Ҳ�������");
    	 }
    	 String jdbcPropertiesPath = PathUitls.getClasspath()+ PropertyUtils.getPro(path, "jdbc.url");
    	
    	 driver = PropertyUtils.getPro(jdbcPropertiesPath, "jdbc.classname");
    	 url = PropertyUtils.getPro(jdbcPropertiesPath, "jdbc.url");
    	 user =  PropertyUtils.getPro(jdbcPropertiesPath, "jdbc.username");
    	 password = PropertyUtils.getPro(jdbcPropertiesPath, "jdbc.password");

		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

	}
    	

	public static PreparedStatement getPStmt(Connection connection,String sql) throws SQLException{
		/*Statement st=connection.createStatement();
		ResultSet rs=st.executeQuery(sql);*/
		PreparedStatement pst=connection.prepareStatement(sql);
	//	pst.execute();
		return pst;
	}

	public static void close(PreparedStatement pstmt) throws SQLException {
		if(pstmt!=null){
			pstmt.close();
		}
		
	}

	public static Connection getConn() throws SQLException {
		return DriverManager.getConnection(url, user, password);
	}

	public static void close(Connection conn) {
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}
